package com.topsun.posclient.finance.view;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import com.topsun.posclient.factory.FinanceFactory;
import com.topsun.posclient.finance.entity.FinanceEntity;
import com.topsun.posclient.finance.tree.DataFactory;

public class FinanceView extends ViewPart {

	public static final String ID = "posclient.FinanceView";

	public TableViewer tv;
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		parent.setSize(500, 500);
		parent.setLayout(new GridLayout());
		SashForm form=new SashForm(parent,SWT.BORDER);

		form.setLayoutData(new GridData(GridData.FILL_BOTH));
		{
			TreeViewer trv=new TreeViewer(form,SWT.BORDER);
			trv.setContentProvider(new TreeViewerContentProvider());
			trv.setLabelProvider(new TreeViewerLabelProvider());
			Object obj=DataFactory.createTreeData();
			trv.setInput(obj);
		}

		{
			//调用方法创建表格
			createTableViewer(form);
			//设定内容器
			tv.setContentProvider(new TableViewerContentFinanceProvider());
			//设定标签器
			tv.setLabelProvider(new TableViewerLabelFinaceProvider());
			//输入数据
			tv.setInput(FinanceFactory.getFinance());
			FinanceActionGroup group =new FinanceActionGroup(tv);
			group.fillContextMenu(new MenuManager());
			tv.setColumnProperties(new String[]{"0","1","2","3","4","5","6","7","8"});
			CellEditor[] cellEditor=new CellEditor[9];
			cellEditor[0]=null;
			cellEditor[1]=new TextCellEditor(tv.getTable());
			cellEditor[2]=new TextCellEditor(tv.getTable());
			cellEditor[3]=new TextCellEditor(tv.getTable());
			cellEditor[4]=new TextCellEditor(tv.getTable());
			cellEditor[5]=new TextCellEditor(tv.getTable());
			cellEditor[6]=new TextCellEditor(tv.getTable());
			cellEditor[7]=new TextCellEditor(tv.getTable());
			cellEditor[8]=new TextCellEditor(tv.getTable());
			tv.setCellEditors(cellEditor);
			tv.setCellModifier(new FinaceCellModeifier(tv));
			Text text=(Text)cellEditor[7].getControl();
			text.addVerifyListener(new VerifyListener(){

				@Override
				public void verifyText(VerifyEvent e) {
					// TODO Auto-generated method stub
					e.doit="0123456789".indexOf(e.text)>0;
				}});
		}
		
		form.setWeights(new int[]{1,4});
	}

	private void createTableViewer(Composite parent)
	{	
		tv=new TableViewer(parent,SWT.BORDER|SWT.MULTI|SWT.FULL_SELECTION);
		Table table=tv.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableLayout tLayout=new TableLayout();
		table.setLayout(tLayout);
		tLayout.addColumnData(new ColumnWeightData(30));
		new TableColumn(table,SWT.NONE).setText("ID");
		tLayout.addColumnData(new ColumnWeightData(40));
		new TableColumn(table,SWT.NONE).setText("结算方式");
		tLayout.addColumnData(new ColumnWeightData(60));
		new TableColumn(table,SWT.NONE).setText("应用到付款");
		tLayout.addColumnData(new ColumnWeightData(60));
		new TableColumn(table,SWT.NONE).setText("应用到收款");
		tLayout.addColumnData(new ColumnWeightData(60));
		new TableColumn(table,SWT.NONE).setText("应用到POS");
		tLayout.addColumnData(new ColumnWeightData(60));
		new TableColumn(table,SWT.NONE).setText("应用到POS费用");
		tLayout.addColumnData(new ColumnWeightData(60));
		new TableColumn(table,SWT.NONE).setText("银行");
		tLayout.addColumnData(new ColumnWeightData(40));
		new TableColumn(table,SWT.NONE).setText("银行账户");
		tLayout.addColumnData(new ColumnWeightData(40));
		new TableColumn(table,SWT.NONE).setText("备注");

	}

	public void addListener()
	{
		tv.addDoubleClickListener(new IDoubleClickListener()
		{

			public void doubleClick(DoubleClickEvent event) {
				// TODO 自动生成方法存根
				IStructuredSelection selection=(IStructuredSelection)event.getSelection();
				FinanceEntity o =(FinanceEntity)selection.getFirstElement();
				MessageDialog.openInformation(null, "提示", o.getId().toString());
			}

		});

	}


	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
